package org.example.service.impl;

import org.example.dao.ClassesDao;
import org.example.dto.ClassDto;
import org.example.dto.MajorDto;
import org.example.pojo.Classes;
import org.example.pojo.Major;
import org.example.service.ClassService;
import org.example.vo.ClassVo;
import org.example.vo.MajorVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ClassServiceImpl implements ClassService {

    @Resource
    private ClassesDao dao;


    private ClassDto PoTransToDto(Classes classes){
        ClassDto classDto=new ClassDto();
        classDto.setClassId(classes.getClassno());
        classDto.setClassName(classes.getClassname());
        classDto.setDepartment(classes.getDepartname());
        classDto.setGrade(classes.getPeriod()+classes.getMajorname());
        classDto.setMajor(classes.getMajorname());
        classDto.setPeopleNumber(classes.getPeople());
        return classDto;
    }

    private Classes VoTransToPo(ClassVo classVo){
        Classes classes=new Classes();
        classes.setClassno(classVo.getClassId());
        classes.setClassname(classVo.getClassName());
        classes.setMajorname(classVo.getMajor());
//        classes.setMajorno();
        classes.setDepartname(classVo.getDepartment());
        classes.setPeople(classVo.getPeopleNumber());
        classes.setPeriod(Integer.valueOf(classVo.getGrade().substring(0,2)));
//        classes.setDepartno();

        return classes;
    }

    @Override
    public Integer addClass(ClassVo classVo) {
        if(dao.insert(VoTransToPo(classVo)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer updateClass(ClassVo classVo) {
        if(dao.update(VoTransToPo(classVo)))
            return 1;
        else
            return 0;
    }

    @Override
    public Integer deleteClass(Integer classId) {
        if(dao.delete(classId))
            return 1;
        else
            return 0;
    }

    @Override
    public List<ClassDto> queryAllClass() {
        List<Classes> classesList=dao.queryAll();
        List<ClassDto> classDtoList=new ArrayList<>();
        for(int i=0;i<classesList.size();i++)
            classDtoList.add(i,PoTransToDto(classesList.get(i)));
        return classDtoList;
    }

    @Override
    public Integer countClass() {
        return dao.queryAll().size();
    }

    @Override
    public List<ClassDto> queryClassByDepartmentAndMajorAndGrade(String department, String major, String grade) {
        Classes classes=new Classes();
        classes.setDepartname(department);
        classes.setMajorname(major);
        List<Classes> classesList=dao.queryByCondition(classes);
        List<ClassDto> classDtoList=new ArrayList<>();
        for(int i=0;i<classesList.size();i++)
            classDtoList.add(i,PoTransToDto(classesList.get(i)));
        return classDtoList;
    }

    @Override
    public ClassDto queryClassById(Integer classId) {
        Classes classes=new Classes();
        classes.setClassno(classId);
        return PoTransToDto(dao.queryByCondition(classes).get(0));
    }

    @Override
    public Integer countClassByDepartmentAndMajorAndGrade(String department, String major, String grade) {
        Classes classes=new Classes();
        classes.setDepartname(department);
        classes.setMajorname(major);
        return dao.queryByCondition(classes).size();
    }

}
